package com.lrkj.framework.websocket.dao.impl;

import com.lrkj.framework.websocket.dao.BaseRedisDao;
import com.lrkj.framework.websocket.dao.PageResult;
import com.lrkj.framework.websocket.dao.RedisPageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public abstract class BaseRedisDaoImpl<T> implements BaseRedisDao<T> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public static final String KEY_PREFIX_VALUE = "bloom:value:key:";
    public static final String KEY_PREFIX_LIST = "bloom:list:key:";
    public static final String KEY_PREFIX_SET = "bloom:set:key:";

    public BaseRedisDaoImpl() {
    }

    public boolean cacheValue(String k, T v, long time) {
        String key = "bloom:value:key:" + k;

        try {
            this.redisTemplate.opsForValue().set(key, v);
            if (time > 0L) {
                this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }

            return true;
        } catch (Throwable var7) {
            this.logger.error("缓存[" + key + "]失败, value[" + v + "]", var7);
            return false;
        }
    }

    public boolean cacheValue(String k, T v) {
        return this.cacheValue(k, v, -1L);
    }

    public boolean containsValueKey(String k) {
        return this.containsKey("bloom:value:key:" + k);
    }

    public boolean containsSetKey(String k) {
        return this.containsKey("bloom:set:key:" + k);
    }

    public boolean containsListKey(String k) {
        return this.containsKey("bloom:list:key:" + k);
    }

    protected boolean containsKey(String key) {
        try {
            return this.redisTemplate.hasKey(key);
        } catch (Throwable var3) {
            this.logger.error("判断缓存存在失败key[" + key + ", error[" + var3 + "]");
            return false;
        }
    }

    public T getValue(String k) {
        try {
            return (T) this.redisTemplate.opsForValue().get("bloom:value:key:" + k);
        } catch (Throwable var3) {
            this.logger.error("获取缓存失败key[bloom:value:key:" + k + ", error[" + var3 + "]");
            return null;
        }
    }

    public boolean removeValue(String k) {
        return this.remove("bloom:value:key:" + k);
    }

    public boolean removeSet(String k) {
        return this.remove("bloom:set:key:" + k);
    }

    public boolean removeList(String k) {
        return this.remove("bloom:list:key:" + k);
    }

    protected boolean remove(String key) {
        try {
            this.redisTemplate.delete(key);
            return true;
        } catch (Throwable var3) {
            this.logger.error("获取缓存失败key[" + key + ", error[" + var3 + "]");
            return false;
        }
    }

    public boolean cacheSet(String k, T v, long time) {
        String key = "bloom:set:key:" + k;

        try {
            this.redisTemplate.opsForSet().add(key, new Object[]{v});
            if (time > 0L) {
                this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }

            return true;
        } catch (Throwable var7) {
            this.logger.error("缓存[" + key + "]失败, value[" + v + "]", var7);
            return false;
        }
    }

    public boolean cacheSet(String k, T v) {
        return this.cacheSet(k, v, -1L);
    }

    public boolean cacheSet(String k, Set<T> v, long time) {
        String key = "bloom:set:key:" + k;

        try {
            this.redisTemplate.opsForSet().add(key, v.toArray((Object[]) (new Object[v.size()])));
            if (time > 0L) {
                this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }

            return true;
        } catch (Throwable var7) {
            this.logger.error("缓存[" + key + "]失败, value[" + v + "]", var7);
            return false;
        }
    }

    public boolean cacheSet(String k, Set<T> v) {
        return this.cacheSet(k, v, -1L);
    }

    public Set<T> getSet(String k) {
        try {
            return (Set<T>) this.redisTemplate.opsForSet().members("bloom:set:key:" + k);
        } catch (Throwable var3) {
            this.logger.error("获取set缓存失败key[bloom:set:key:" + k + ", error[" + var3 + "]");
            return null;
        }
    }

    public boolean cacheList(String k, T v, long time) {
        String key = "bloom:list:key:" + k;

        try {
            this.redisTemplate.opsForList().rightPush(key, v);
            if (time > 0L) {
                this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }

            return true;
        } catch (Throwable var7) {
            this.logger.error("缓存[" + key + "]失败, value[" + v + "]", var7);
            return false;
        }
    }

    public boolean cacheList(String k, T v) {
        return this.cacheList(k, v, -1L);
    }

    public boolean cacheList(String k, List<T> v, long time) {
        String key = "bloom:list:key:" + k;

        try {
            this.redisTemplate.opsForList().rightPushAll(key, new Object[]{v});
            if (time > 0L) {
                this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }

            return true;
        } catch (Throwable var7) {
            this.logger.error("缓存[" + key + "]失败, value[" + v + "]", var7);
            return false;
        }
    }

    public boolean cacheList(String k, List<T> v) {
        return this.cacheList(k, v, -1L);
    }

    public List<T> getList(String k, long start, long end) {
        try {
            return (List<T>) this.redisTemplate.opsForList().range("bloom:list:key:" + k, start, end);
        } catch (Throwable var7) {
            this.logger.error("获取list缓存失败key[bloom:list:key:" + k + ", error[" + var7 + "]");
            return null;
        }
    }

    public long getListSize(String k) {
        try {
            return this.redisTemplate.opsForList().size("bloom:list:key:" + k);
        } catch (Throwable var3) {
            this.logger.error("获取list长度失败key[bloom:list:key:" + k + "], error[" + var3 + "]");
            return 0L;
        }
    }

    public T removeOneOfList(String k) {
        try {
            return (T) this.redisTemplate.opsForList().rightPop("bloom:list:key:" + k);
        } catch (Throwable var3) {
            this.logger.error("移除list缓存失败key[bloom:list:key:" + k + ", error[" + var3 + "]");
            return null;
        }
    }

    public PageResult<T> getPageResult(RedisPageQuery query) {
        String key = query.getKey();
        PageResult<T> pr = new PageResult();
        pr.setCurrentIndex((long) query.getOffset());
        pr.setPageSize(query.getLimit());
        pr.setTotalCount((long) ((int) this.getListSize(key)));
        pr.setTotalPage((int) ((pr.getTotalCount() + (long) query.getLimit() - 1L) / (long) query.getLimit()));
        pr.setCurrentPage((query.getOffset() + query.getLimit()) / query.getLimit());
        if (!this.containsListKey(key)) {
            pr.setContent((List) null);
            return pr;
        } else {
            pr.setContent(this.getList(key, (long) query.getOffset(), (long) (query.getOffset() + query.getLimit())));
            return pr;
        }
    }
}
